<?php
    function exceptionErrorHandler( $ex )
    {
        $lastInfo = '';
        $fileName = 'logs/error_log.txt';
        if ( file_exists ( $fileName ) )
        {
            $lastInfo = "\r\n\r\n" . file_get_contents( 'logs/error_log.txt' );
        }
        $logFile = fopen( $fileName, "w" );
        date_default_timezone_set( 'Europe/Moscow' );
        $newInfo = date( "[ d-m-Y H:i:s ]" ) . "\r\n";
        $newInfo .= 'Error Message: ' . $ex->getMessage() . $lastInfo;
        fwrite( $logFile, $newInfo );
        fclose( $logFile );
        
        if ( !headers_sent() )
        {
            header( "Location: /php/error.php" );
        }
        return true;
    }

	function globalErrorHandler( $errno, $errstr, $errfile, $errline )
    {
        return exceptionErrorHandler( new Exception( $errstr ) );
    }

    set_exception_handler( 'exceptionErrorHandler' );
    set_error_handler( 'globalErrorHandler' );
?>